#include "TimeWidget.h"
#include "ui_TimeWidget.h"
#include "Constant.h"
#include "mainapp.h"
#include "SqliteDao.h"
#include <QTimer>
#include <QDateTime>
#include <QPainter>

TimeWidget::TimeWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TimeWidget)
{
    ui->setupUi(this);
    player = new QMediaPlayer;
//    player->setMedia(QUrl::fromLocalFile("/home/platinum/音乐/dingDong_daZhong.wav"));
    player->setMedia(QUrl("qrc:/media/media/daZhong.wav"));
//    player->set

    player->setVolume(50);
    /* player->play(); */
}

TimeWidget::~TimeWidget()
{
    delete ui;
}
void TimeWidget::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QColor colorPink(255,136,240);
    QPen pen(colorPink,4);
    painter.setPen(pen);
    painter.drawArc(QRect(10,10,Constant::TW-20,Constant::TW-30),startAngle,timeDownLength);
}
void TimeWidget::init(TimeEntity timeData){
    this->timeData = timeData;
    ui->labelTitle->setText(timeData.title);
    this->initTimes();
    timer = new QTimer(this);
    timer->setInterval(timeData.refreshTime);
    timer->stop();
    connect(timer,SIGNAL(timeout()),this,SLOT(on_timer_timeout()));
}
void TimeWidget::initTimes(){
    hour = timeData.hour;
    minute = timeData.minute;
    second = timeData.second;
    if(second > 60){
        second = second - 60;
        minute ++;
    }
    if(minute > 60){
        minute = minute - 60;
        hour ++;
    }
    if(hour > 99){
        hour = 99;
    }
    totalSecond = hour * 3600 + minute * 60 + second;
    totalMillisecond = (long) (totalSecond * 1000);
    timeSurplus = totalMillisecond;
    totalSecond = hour * 3600 + minute * 60 + second;
    repeatTime();
}
/**
 * 刷新时间
 */
void TimeWidget::timeToStr(){
   QString str;
   if(hour < 10){str.append(Constant::STR_ZERO);}
   str.append(QString::number(hour));
//   str.append(hour);//错误写法
   str.append(Constant::STR_MAO_HAO);
   if(minute < 10){str.append(Constant::STR_ZERO);}
   str.append(QString::number(minute));
   str.append(Constant::STR_MAO_HAO);
   if(second < 10){str.append(Constant::STR_ZERO);}
   str.append(QString::number(second));
//   str.toLatin1().data()   str.toLocal8Bit().data()
//    printf("a==1==%d:%d:%d ==2== %s--3--%d\n",
//           hour,minute,second,str.toStdString().data(),timeData.refreshTime);
   ui->labelTime->setText(QString::fromUtf8(str.toUtf8()));
}

void TimeWidget::repeatTime(){
    //刷新时间
   timeToStr();
   //刷新进度
    repaint();
}
/**
 * 时间到了、重置
 */
void TimeWidget::timeOver(){
    timer->stop();
    isRunning = false;
    changeBtnEditIcon(true);
    changeBtnRSIcon();
}
void TimeWidget::changeBtnEditIcon(bool flag){
    QIcon icon;
    QString str = flag?Constant::IMG_EDIT_BLUE:Constant::IMG_EDIT_GREY;
    icon.addFile(str, QSize(), QIcon::Normal, QIcon::Off);
    ui->btnEdit->setEnabled(flag);
    ui->btnEdit->setIcon(icon);
}
void TimeWidget::changeBtnRSIcon(){
    QIcon icon;
    QString str = timer->isActive()?Constant::IMG_STOP:Constant::IMG_START;
    icon.addFile(str, QSize(), QIcon::Normal, QIcon::Off);
    ui->btnRS->setIcon(icon);
}
void TimeWidget::on_btnDel_clicked()
{
    Constant::objList.removeOne(this);
    SqliteDao::instance()->sqlDelete(&timeData.title);
    timer->stop();
    player->stop();
    delete this;
}
void TimeWidget::on_timer_timeout(){
    //计算剩余时分秒
    timeSurplus = totalMillisecond - (QDateTime::currentMSecsSinceEpoch() - timeStart);
    hour = (int) (timeSurplus / ih);
    tm = timeSurplus - hour * ih;
    minute = (int) (tm /im);
    second = (int) ((tm - minute * im) / is) + 1;
    //计算圆弧
    tmpArc = (double)timeSurplus / totalMillisecond;
    timeDownLength = tmpArc * timeDown360;
    startAngle = startAngleParam - timeDownLength;
//    printf("%ld\n",QDateTime::currentMSecsSinceEpoch());
    if (timeSurplus < 1){
        timeDownLength = 0;
        second = 0;
        timeSurplus = 0;
        timeOver();
        player->play();
    }
    repeatTime();
}
void TimeWidget::on_btnRS_clicked()
{
    if(isRunning){
        if(timer->isActive()){//暂停
            timer->stop();
            timePauseStart = QDateTime::currentMSecsSinceEpoch();
        }else{//开始
            timer->start();
            //再次开始时向后推移开始计时的时间点
            timeStart += QDateTime::currentMSecsSinceEpoch() - timePauseStart;
        }
    }else{
        timeStart = QDateTime::currentMSecsSinceEpoch();
        isRunning = true;
        changeBtnEditIcon(false);
        step = timeDown360/totalSecond;
        timer->start();
    }
    changeBtnRSIcon();
}

void TimeWidget::on_btnReset_clicked()
{
   timeDownLength = timeDown360;
   startAngle = 0;
   initTimes();
   timeOver();
   player->stop();
}

void TimeWidget::on_btnEdit_clicked()
{
    MainApp::mainApp->isEdit = true;
    MainApp::mainApp->timeDataEdit(this);
}
